package com.controller;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.domain.vo.PriceTypeVo;
import com.domain.vo.ProductStatisticVo;
import com.domain.vo.ProductTop20Vo;
import com.result.Result;
import com.result.Results;
import com.service.ReportService;
import com.utils.DateRangeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

/**
 * 数据统计相关接口
 */
@RestController
@RequestMapping("/report")
@Slf4j
public class ReportController {

    @SofaReference(interfaceType = ReportService.class, binding = @SofaReferenceBinding(bindingType = "bolt"))
    private ReportService reportService;


    /**
     * 统计指定时间段内，每天的兑换量
     * @return
     */
    @GetMapping("/productStatistic")
    public Result<List<ProductStatisticVo>> getTotalExchangeAmountByDateRange(
            @RequestParam("begin") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam("end") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate)  {

        //日期校验
        LocalDate[] validatedDates = DateRangeUtil.getValidatedDateRange(startDate, endDate);
        startDate = validatedDates[0];
        endDate = validatedDates[1];

        log.info("统计指定时间段内，每天的兑换量，开始日期：{}，结束日期：{}", startDate, endDate);
        List<ProductStatisticVo> result = reportService.getProductStatisticVoByDateRange(startDate, endDate);
        return Results.success(result);
    }

    /**
     * 统计指定时间段内，每日兑换方式兑换量
     * @param
     * @param
     * @return
     */
    @GetMapping("/priceType")
    public Result<List<PriceTypeVo>> priceType(
            @RequestParam("begin") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam("end") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {

        //日期校验
        LocalDate[] validatedDates = DateRangeUtil.getValidatedDateRange(startDate, endDate);
        startDate = validatedDates[0];
        endDate = validatedDates[1];

        log.info("统计指定时间段内，每日兑换方式兑换量，开始日期：{}，结束日期：{}", startDate, endDate);
        List<PriceTypeVo> result = reportService.getPriceTypeByDateRange(startDate, endDate);
        return Results.success(result);
    }


    /**
     * 统计指定时间段内，销售前20的商品分类
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/top20")
    public Result productTop20(@RequestParam("begin") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
                               @RequestParam("end") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {

        //日期校验
        LocalDate[] validatedDates = DateRangeUtil.getValidatedDateRange(startDate, endDate);
        startDate = validatedDates[0];
        endDate = validatedDates[1];

        log.info("统计指定时间段内，销售前20的商品分类，开始日期：{}，结束日期：{}", startDate, endDate);

        List<ProductTop20Vo> result=reportService.getProductTop20ByDateRange(startDate,endDate);
        return Results.success(result);
    }

}
